package com.lipy.springootrabbitmq.config.deadLetter;

import com.lipy.springootrabbitmq.config.DLXRabbitMQConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = DLXRabbitMQConfig.DEAD_LETTER_QUEUE)
public class DeadLetterConsumer {

    @RabbitHandler
    public void consumer(String msg, Channel channel, Message message){
        System.out.println("DeadLetterConsumer.consumer");
        try {
            // 拒绝后会转发到配置的 x-dead-letter-exchange x-dead-letter-routing-key 上
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(1);
    }
}
